package com.itheima.treesetdemo;

import java.util.Iterator;
import java.util.TreeSet;

/**
 * Tree 集合存储Student类型
 */
public class TreeSetDemo2 {
    public static void main(String[] args) {
        TreeSet<Student> ts = new TreeSet<>();

        Student s1 = new Student("zhangsan", 28);
        Student s2 = new Student("lisi", 27);
        Student s3 = new Student("wangwu", 29);
        Student s4 = new Student("zhaoliu", 28);
        Student s5 = new Student("qianqi", 30);

        // 想使用 TreeSet，必须指定排序规则
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);

        System.out.println(ts);

        System.out.println("-----------------------------------------------");

        Iterator<Student> it = ts.iterator();
        while (it.hasNext()) {
            Student stu = it.next();
            System.out.println(stu);
        }
    }
}
